<?php
defined ( 'BASEPATH' ) or exit ( 'No direct script access allowed' );
class Project extends CI_Controller {
	public function __construct() {
		parent::__construct ();
		$this->admin->isLogin ( site_url ( 'login' ) );
		$this->load->model ( 'project_model' );
		$this->load->model('user_model');
		$this->load->model('category_model');
	}
	/*首页*/
	public function index($page=1){
		$whereArr = array('status'=>1);
		$count = $this->project_model->count ( $whereArr );
		$params = array(
				"url_template" => site_url ( 'Project/index/{page}' ),
				"page_count" => $count,
				"page_now" => $page,
				"page_size" => 10
		);
		$params["total_pages"] = ceil ( $params['page_count'] / $params['page_size'] );
		$this->load->library ( 'paging', $params );
		$project_list = $this->project_model->result ( $whereArr, $page, $params['page_size'] );
		foreach($project_list as $key => $value){
			$project_list[$key]['uid'] = $this->change_username($value['uid']);
			$project_list[$key]['cid'] = $this->changeCategory($value['cid']);
		}
		$data['project_list'] = $project_list;
		$this->load->view('project_index',$data);
	}
	/*
	 *根据用户id查找用户名
	 */
	public function change_username($uid=1){
		$whereArr = array("uid"=>$uid);
		$result = $this->user_model->row($whereArr);
		return $result['username'];
	}
	/*
	 *替换栏目id为栏目名
	 */
	public function changeCategory($cid=1){
		$whereArr = array("cid"=>$cid);
		$result = $this->category_model->row($whereArr);
		return $result['cname'];
	}
	/*
	 *删除项目操作 
	 */
	public function del($pid){
		$whereArr = array("pid"=>$pid);
		$dataArr = array("status"=>0);
		$result = $this->project_model->update($dataArr,$whereArr);
		if($result){
			$data['status'] = 1;
			$data['info'] = "众创项目删除成功!";
 		}else{
			$data['status'] = 0;
			$data['info'] = "众创项目删除失败!";
		}
		echo json_encode($data);
	}
	/*
	 *删除多个众创项目 
	 */
	public function duodel(){
		$ids = $this->input->post('ids');
		if(empty($ids))
		{
			$flag =array('status'=>-1,'info'=>'请选择数据!!');
		}else{
			$dataArr=array("status"=>0);
			$result=$this->project_model->update_in($dataArr,'pid',$ids);
			if($result){
				$flag = array('status'=>1,'info'=>'众创项目删除成功!');
			}else{
				$flag = array('status'=>0,'info'=>'众创项目删除失败!');
			}
		}
		echo json_encode($flag);
	}
}